package 字符串.middle;

/**
 * @author Lw中
 * @date 2020/8/27 11:02
 */
public class QiJiuYi {

    /**
     * LWZ TODO : 2020/8/27
     * 三个循环中第二第三个循环无法理解
     */
    public static String customSortString(String S, String T) {
        StringBuilder sb=new StringBuilder();
        int []cnt=new int[26];
        for (char i:T.toCharArray()) {
            cnt[i-'a']++;
        }

        for (int i=0;i<S.length();i++){
            for (int j=0;j<cnt[S.charAt(i)-'a'];j++){
                sb.append(S.charAt(i));
            }
            cnt[S.charAt(i)-'a']=0;
        }
        for (int i=0;i<26;i++){
            if (cnt[i]!=0){
                for (int j=0;j<cnt[i];j++){
                    sb.append((char)(i+'a'));
                }
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String s = customSortString("cba", "abcd");
        System.out.println(s);
    }

}
